package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Collection接口中声明的方法的测试
 * 结论：
 * 向Collection接口的实现类的对象中添加数据obj时，要求obj所在类要重写equals().
 *
 * @author : leimingchao
 * @date Date : 2021年10月05日 21:30
 */
public class CollectionTest {

    @Test
    public void test1() {

        Collection collection = new ArrayList();
        collection.add(123);
        collection.add(456);
        // Person person = new Person("Jerry", 20);
        // collection.add(person);
        collection.add(new Person("Jerry", 20));
        collection.add(new String("Tom"));
        collection.add(false);
        //contains(Object obj):判断当前集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals()
        boolean contains = collection.contains(123);
        System.out.println(contains);
        System.out.println(collection.contains(new String("Tom")));
        //true
        // System.out.println(collection.contains(person));
        //false ----> true
        System.out.println(collection.contains(new Person("Jerry", 20)));
        //2.containsAll(Collection collection1):判断形参collection1中的所有元素是否都存在于当前集合中。
        Collection collection1 = Arrays.asList(123, 4567);
        System.out.println(collection.containsAll(collection1));
    }

    @Test
    public void test2() {
        //3.remove(Object obj)；从当前集合中删除obj元素
        Collection collection = new ArrayList();
        collection.add(123);
        collection.add(456);
        collection.add(new Person("Jerry", 20));
        collection.add(new String("Tom"));
        collection.add(false);

        collection.remove(1234);
        System.out.println(collection);
        collection.remove(new Person("Jerry", 20));
        System.out.println(collection);
        //4.removeAll(Collection collection1):差集：从当前集合中移除collection1中所有的元素，并把结果返回给当前集合
        Collection collection1 = Arrays.asList(123, 456);
        collection.removeAll(collection1);
        System.out.println(collection);

    }

    @Test
    public void test3() {
        Collection collection = new ArrayList();
        collection.add(456);
        collection.add(123);
        collection.add(new Person("Jerry", 20));
        collection.add(new String("Tom"));
        collection.add(false);
        //5.retainAll(Collection collection1):交集：获取当前集合和collection1集合的交集，并把结果返回给当前集合
        // Collection collection1 = Arrays.asList(123,456,789);
        // collection.retainAll(collection1);
        // System.out.println(collection);

        //6.equals(Object obj):要想返回true，需要当前集合和形参集合的元素都相同
        Collection collection1 = new ArrayList();
        collection1.add(123);
        collection1.add(456);
        collection1.add(new Person("Jerry", 20));
        collection1.add(new String("Tom"));
        collection1.add(false);
        System.out.println(collection.equals(collection1));
    }

    @Test
    public void test4() {
        Collection collection = new ArrayList();
        collection.add(123);
        collection.add(456);
        collection.add(new Person("Jerry", 20));
        collection.add(new String("Tom"));
        collection.add(false);
        //7.hashCode():返回当前对象的哈希值
        System.out.println(collection.hashCode());
        //8.集合 ---> 数组：toArray()
        Object[] arr = collection.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //拓展：数组 ----> 集合:调用Arrays类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
        System.out.println(list);

        List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
        //1
        System.out.println(arr1.size());

        List<Integer> arr2 = Arrays.asList(new Integer[]{123, 456});
        System.out.println(arr2);
        System.out.println(arr2.size());
        //9.iterator():返回Iterator接口的实例，用于遍历集合元素。放在IteratorTest.java中测试
    }

}
